import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

final dbHelp = DbHelp();

class DbHelp {
  factory DbHelp() => _getInstance();

  static DbHelp get instance => _getInstance();
  static DbHelp _instance;

  DbHelp._internal() {
    database = _openDb();
  }

  static DbHelp _getInstance() {
    if (_instance == null) {
      _instance = DbHelp._internal();
    }
    return _instance;
  }

  Future<Database> database;

  Future<Database> _openDb() async {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'demo.db');
    return await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {
      await db.execute("""
      CREATE TABLE user_info (
        id INT PRIMARY KEY NOT NULL,
        avatar VARCHAR(100) NOT NULL,
        name VARCHAR(20) NOT NULL,
        note VARCHAR(50)
      )""");
      await db.execute("""
      CREATE TABLE todo_collect (
        id CHAR(32) PRIMARY KEY NOT NULL,
        dateTime BIGINT NOT NULL,
        filePath VARCHAR(100) NOT NULL,
        indexNum SMALLINT NOT NULL
      )""");
      await db.execute("""
      CREATE TABLE todo_list (
        id CHAR(32) PRIMARY KEY NOT NULL, 
        title VARCHAR(16) NOT NULL, 
        note VARCHAR(101), 
        taskTime BIGINT NOT NULL, 
        collectId CHAR(32) NOT NULL
      )""");
    });
  }
}
